package com.alex.server.service.impl;

import com.alex.server.domain.User;
import com.alex.server.dto.LoginUserDto;
import com.alex.server.dto.UserDto;
import com.alex.server.dto.PageDto;
import com.alex.server.mapper.UserMapper;
import com.alex.server.service.IUserService;
import com.alex.server.util.CopyUtil;
import com.alex.server.util.TokenUtil;
import com.alex.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public PageDto list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        List<User> userList = mapper.selectByExample(null);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        pageDto.setTotal(pageInfo.getTotal());
        List<UserDto> userDtoList = CopyUtil.copyList(userList, UserDto.class);
        pageDto.setList(userDtoList);
        return pageDto;
    }

    @Override
    public void save(UserDto userDto) {
        User user = CopyUtil.copy(userDto, User.class);
        if (StringUtils.isEmpty(userDto.getId())){
            this.insert(user);
        } else {
            this.update(user);
        }
    }

    @Override
    public void insert(User user) {
        user.setId(UuidUtil.getShortUuid());
        mapper.insert(user);
    }

    @Override
    public void update(User user) {
        mapper.updateByPrimaryKey(user);
    }

    @Override
    public void delete(String id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public String loginSuccess(LoginUserDto loginUserDto, HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchAlgorithmException {

        HttpSession session = request.getSession();

        String identity = loginUserDto.getIdentity();

        String pStr = loginUserDto.getpStr();

        // TODO 登录失败待开发
        String code = "0";

        session.removeAttribute("token");

        User user = mapper.selectByUserName(identity);

        if (pStr.equals(user.getPassword())) {
            String token = TokenUtil.createToken(identity, pStr);

            session.setAttribute("token", token);
            //获取session的Id
            String sessionId = session.getId();

            //将session的Id存储到名字为JSESSIONID的cookie中
            Cookie cookie = new Cookie("JSESSIONID", sessionId);
            //设置cookie的有效路径
            cookie.setPath(request.getContextPath());
            response.addCookie(cookie);
            return token;
        }

        return code;
    }

}